// to JSON File require ABI(binary) 
import voting from "./Create.json"

// HARDHAT LOCALHOST
// export const VotingAddress = "0x5FbDB2315678afecb367f032d93F642f64180aa3";
// 可单独放在 types/global.d.ts 或者当前文件顶部（根据项目结构灵活调整）


/**
 * sepolia arbitrum合约和owner地址
 * 0xa8592EbFd31247BE545eA42c10BfF47288Cc17cf
 */

export const VotingAddress = "0x5FbDB2315678afecb367f032d93F642f64180aa3";

export const CONTRACT_OWNER = voting.abi;

// POLYGON TEST
// export const VotingAddress = "0x5FbDB2315678afecb367f032d93F642f64180aa3"

export const VotingAddressABI = voting.abi

// CONTRACR OWNER
// HARDHAT 
// export const CONTRACT_OWNER = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266";

// REAL ADDRESS
// export const CONTRACT_OWNER = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266";

// NETWORK

/**
 * Infura项目ID，用于连接以太坊网络
 */
const infuraId = "3436d3a6c498444eb22be2c52c79c7d9";
/**
 * 支持的区块链网络配置
 * 包含网络ID、名称、货币信息、RPC节点和区块浏览器URL
 */
const network = {
    polygon_amoy: {
        chainId: `0x${Number(80002).toString(16)}`,
        chainName: "Amoy",
        nativeCurrency: {
            name: "POL",
            symbol: "POL",
            decimals: 18,
        },
        rpcUrls: ["https://rpc-amoy.polygon.technology/"],
        blockExplorerUrls: ["https://www.oklink.com/amoy"]
    },
    arbitrum: {
        chainId: `0x${Number(421614).toString(16)}`,
        chainName: "Arbitrum Sepolia",
        nativeCurrency: {
            name: "ETH",
            symbol: "ETH",
            decimals: 18,
        },
        rpcUrls: ["https://sepolia-rollup.arbitrum.io/rpc"],
    },
    polygon_mumbai: {
        chainId: `0x${Number(80001).toString(16)}`,
        chainName: "Polygon Mumbai",
        nativeCurrency: {
            name: "MATIC",
            symbol: "MATIC",
            decimals: 18,
        },
        rpcUrls: ["https://rpc.ankr.com/polygon_mumbai"],
        blockExplorerUrls: ["https://mumbai.polygonscan.com/"]
    },
    ploygon: {
        chainId: `0x${Number(137).toString(16)}`,
        chainName: "Polygon",
        nativeCurrency: {
            name: "POL",
            symbol: "POL",
            decimals: 18,
        },
        rpcUrls: ["https://polygon-mainnet.infura.io"+infuraId],
        blockExplorerUrls: ["https://polygonscan.com"],
    },
    bsc: {
        chainId: `${Number(56).toString(16)}`,
        chainName: "Binance Smart Chain Mainnet",
        nativeCurrency: {
            name: "Binance Chain Native Token",
            symbol: "BNB",
            decimals: 18,
        },
        rpcUrls: ["https://bsc-mainnet.infura.io/v3/" + infuraId],
        blockExplorerUrls: ["https://bscscan.com"]
    },
    base_mainnet: {
        chainId: `0x${Number(8453).toString}`,
        chainName: "Base Mainnet",
        nativeCurrency: {
            name: "ETH",
            symbol: "ETH",
            decimals: 18,
        },
        rpcUrls: ["https://base-mainnet.infura.io"],
        blockExplorerUrls: ["https://bscscan.org"],
    },
    base_sepolia: {
        chainId: `0x${Number(84532).toString}`,
        chainName: "Base Sepolia",
        nativeCurrency: {
            name: "ETH",
            symbol: "ETH",
            decimals: 18,
        },
        rpcUrls: ["https://base-sepolia.infura.io/v3/" + infuraId],
        blockExplorerUrls: ["https://bscscan.com"],
    },
    localhost: {
        chainId: `0x${Number(31337).toString(16)}`,
        chainName: "GoChain Testnet",
        nativeCurrency: {
            name: "GO",
            symbol: "GO",
            decimals: 18,
        },
        rpcUrls: ["http://127.0.0.1:8545/"],
        blockExplorerUrls: ["https://bscscan.com"],
    }
}
/**
 * 切换以太坊网络的异步函数
 * @param networName - 目标网络名称，对应network对象中的键
 */
const changeNetwork = async ({ networkName }: { networkName: keyof typeof network }) => {
    try {
        // 检查是否存在以太坊钱包扩展
        if (!window.ethereum) throw new Error("No crypto wallet found")
        // 请求钱包添加并切换到指定网络
        await window.ethereum.request({
            method: "wallet_addEthereumChain",
            params: [
                {
                    ...network[networkName],
                },
            ]
        });


    } catch (error: any) {
        console.log(error.message)
    }
}
/**
 * 处理网络切换的函数（默认切换到localhost网络）
 * 可在UI组件中调用此函数
 */
export const handleNetworkSwitch = async () => {
    const networkName: keyof typeof network = 'localhost';
    await changeNetwork({ networkName })
}
//END  OF NETWORK-------